package leetcode9;

public class test47 {
    /**
     * 200. 岛屿数量
     * @param grid
     * @return
     */
    //DFS
    public int numIslands(char[][] grid) {
        int count = 0;
        for(int r = 0;r < grid.length;r++){
            for(int c = 0;c < grid[0].length;c++){
                if(grid[r][c] == '1'){
                    dfs(grid,r,c);
                    count++;
                }

            }
        }
        return count;
    }
    public void dfs(char[][] grid,int r,int c){
        if(!isArea(grid,r,c))
            return;
        if(grid[r][c] != '1')
            return;
        grid[r][c] = '2';
        dfs(grid,r,c + 1);
        dfs(grid,r,c - 1);
        dfs(grid,r - 1,c);
        dfs(grid,r + 1,c);
    }
    public boolean isArea(char[][] grid,int r,int c){
        return r >= 0 && r < grid.length && c >= 0 && c < grid[0].length;
    }
}
